package com.itheima.controller;


import aliyun.oss.AliOSSUtils;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 上传文件
     * @param image
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception{
        log.info("image={}",image);

        /*//获取原始文件名
        String originalFilename = image.getOriginalFilename();

        //构建新的文件名  通过UUID获得特殊的文件名前缀
        String newFileName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));


        //将文件保存在服务器端E:/images/目录下
        image.transferTo(new File("D:/images"+newFileName));
        return Result.success();
*/

        String url = aliOSSUtils.upload(image); //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        return Result.success(url);  //将图片上传完成后的url返回，用于浏览器回显显示
    }
}
